package com.zillion.health.service.impl;

import java.util.Collection;
import java.util.List;

import com.zillion.common.constant.CacheConstants;
import com.zillion.common.constant.UserConstants;
import com.zillion.common.core.text.Convert;
import com.zillion.common.exception.ServiceException;
import com.zillion.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zillion.health.mapper.HealthConfigMapper;
import com.zillion.health.domain.HealthConfig;
import com.zillion.health.service.IHealthConfigService;
import com.zillion.common.core.redis.RedisCache;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;

import static com.zillion.common.utils.DictUtils.getCacheKey;

/**
 * 从业人员配置Service业务层处理
 *
 * @author zillion
 * @date 2025-06-12
 */
@Service
public class HealthConfigServiceImpl implements IHealthConfigService
{
    @Autowired
    private HealthConfigMapper healthConfigMapper;
    @Autowired
    private RedisCache redisCache;

    /**
     * 项目启动时，初始化参数到缓存
     */
    @PostConstruct
    public void init() {
        loadingConfigCache();
    }

    /**
     * 清空缓存
     */
    @Override
    public void clearConfigCache() {
        Collection<String> keys = redisCache.keys(CacheConstants.HEALTH_CONFIG_KEY + "*");
        redisCache.deleteObject(keys);
    }

    /**
     * 初始化参数缓存
     */
    @Override
    public void loadingConfigCache() {
        List<HealthConfig> configsList = healthConfigMapper.selectHealthConfigList(new HealthConfig());
        for (HealthConfig config : configsList) {
            redisCache.setCacheObject(getCacheKey(config.getKey()), config.getValue());
        }
    }
    /**
     * 重置从业人员配置缓存数据
     */

    public void resetConfigCache() {
        clearConfigCache();
        loadingConfigCache();
    }
    /**
     * 根据键名查询从业人员配置信息
     *
     * @param configKey 参数key
     * @return 参数键值
     */

    public String selectConfigByKey(String configKey) {
        String configValue = Convert.toStr(redisCache.getCacheObject(getCacheKey(configKey)));
        if (StringUtils.isNotEmpty(configValue)) {
            return configValue;
        }
        HealthConfig config = new HealthConfig();
        config.setKey(configKey);
        HealthConfig retConfig = (HealthConfig) healthConfigMapper.selectHealthConfigList(config);
        if (StringUtils.isNotNull(retConfig)) {
            redisCache.setCacheObject(getCacheKey(configKey), retConfig.getValue());
            return retConfig.getValue();
        }
        return StringUtils.EMPTY;
}
        /**
         * 查询从业人员配置
         *
         * @param id 从业人员配置主键
         * @return 从业人员配置
         */
    @Override
    public HealthConfig selectHealthConfigById(Long id)
    {
//        String configValue = Convert.toStr(redisCache.getCacheObject(getCacheKey(id)));

        return healthConfigMapper.selectHealthConfigById(id);
    }

    /**
     * 查询从业人员配置列表
     *
     * @param healthConfig 从业人员配置
     * @return 从业人员配置
     */
    @Override
    public List<HealthConfig> selectHealthConfigList(HealthConfig healthConfig)
    {
        healthConfig.setParams(redisCache.getCacheMap(CacheConstants.HEALTH_CONFIG_KEY));
        return healthConfigMapper.selectHealthConfigList(healthConfig);
    }

    /**
     * 新增从业人员配置
     *
     * @param healthConfig 从业人员配置
     * @return 结果
     */
    @Override
    public int insertHealthConfig(HealthConfig healthConfig)
    {

        int row= healthConfigMapper.insertHealthConfig(healthConfig);
        if (row > 0) {
            redisCache.setCacheObject(getCacheKey(healthConfig.getKey()), healthConfig.getValue());
        }
        return row;

    }

    /**
     * 修改从业人员配置
     *
     * @param healthConfig 从业人员配置
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateHealthConfig(HealthConfig healthConfig)
    {
        HealthConfig temp = healthConfigMapper.selectHealthConfigById(healthConfig.getId());
//        if (!StringUtils.equals(temp.getKey(), healthConfig.getKey())) {
            redisCache.deleteObject(getCacheKey(temp.getKey()));
//        }

        int row= healthConfigMapper.updateHealthConfig(healthConfig);
        if (row>0){
            redisCache.setCacheObject(getCacheKey(healthConfig.getKey()), healthConfig.getValue());

        }
        return row;
    }

    /**
     * 批量删除从业人员配置
     *
     * @param ids 需要删除的从业人员配置主键
     * @return 结果
     */
    @Override
    public void deleteHealthConfigByIds(Long[] ids)
    {

        for (Long configId : ids){
            HealthConfig config = selectHealthConfigById(configId);

            healthConfigMapper.deleteHealthConfigById(configId);

             redisCache.deleteObject(getCacheKey(config.getKey()));
        }

    }

    /**
     * 删除从业人员配置信息
     *
     * @param id 从业人员配置主键
     * @return 结果
     */
    @Override
    public int deleteHealthConfigById(Long id)
    {    HealthConfig config = selectHealthConfigById(id);
        if (config != null) {
            redisCache.deleteObject(getCacheKey(config.getKey()));
        }
        return healthConfigMapper.deleteHealthConfigById(id);
    }

    private String getCacheKey(String configKey) {
        return CacheConstants.HEALTH_CONFIG_KEY + configKey;
    }
}
